<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="pe.lucene.web.myrating.HibernateUtil"%>
<%@page import="pe.lucene.web.myrating.Player"%>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="com.oreilly.servlet.MultipartRequest,
                com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
                
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<link rel="stylesheet" type="text/css" href="index.css" />
<title>Regist Ok</title>
</head>
<meta http-equiv="refresh" content="4;url=../index.jsp">
<body>

<%
	String id = request.getParameter("id");
	String passwd = request.getParameter("passwd");
	String backnum = request.getParameter("backnum");
	String position = request.getParameter("position");
	String team = request.getParameter("team");
	
	String fileName = null;
	String savePath = "/Users/lucene/Documents/workspace/WebMyRating/WebContent/uploadprofile";
	int sizeLimit = 5 * 1024 * 1024; //5MB
	
	try {
		MultipartRequest multi = new MultipartRequest(request, savePath, sizeLimit, new DefaultFileRenamePolicy());
		Enumeration formNames = multi.getFileNames(); //return form name
		String formName = (String) formNames.nextElement();
		fileName = multi.getFilesystemName(formName); // return file name
		
		out.print("origin name:" + multi.getOriginalFileName(formName) );
		
		if (fileName == null) { //file not upload
			out.print("File Not Uploaded");
		} else {
			fileName = new String(fileName.getBytes("8859_1"), "UTF-8");
				
			id = multi.getParameter("id");
			passwd = multi.getParameter("passwd");
			backnum = multi.getParameter("backnum");
			position = multi.getParameter("position");
			team = multi.getParameter("team");
		}
	} catch (Exception e) {
		out.println("Exception Occured");
	}
	
	Session hbSession = null;
	Transaction tx = null;
	
	try {
		hbSession = HibernateUtil.currentSession();
		tx = hbSession.beginTransaction();
		
		Player player = new Player();
		player.setId(id);
		player.setPasswd(passwd);
		if (backnum != null) {
			player.setBacknum(Integer.parseInt(backnum));
		}
		
		if (position != null) {
			player.setPosition(Integer.parseInt(position));
		}
		
		if (fileName != null) {
			player.setPix("uploadprofile/" + fileName);
		}
		
		player.setTeam(team);
		
		hbSession.save(player);
		out.println("Session saved<BR>");
		tx.commit();
		
		player.setLogged(true);
		session.setAttribute("player", player);
		out.println( "setAttributed] now:" + player + ", attribute:" + session.getAttribute("player"));
		
	} catch(Exception ex) {
		session.removeAttribute("player");
		out.println("Exception catch");
		out.println(ex.getMessage());
		if (tx != null) tx.rollback();
	} finally {
		HibernateUtil.closeSession();
		out.println("Finally close session");
	}
%>

</body>
</html>